<?php
require_once("database.php");
class xl_thanh_vien extends database
{
	function thanh_vien($ten_dang_nhap,$mat_khau)
	{
		$ten_dang_nhap = addslashes($ten_dang_nhap);
		$mat_khau =md5(addslashes($mat_khau));
		$sql = "select * from thanh_vien where ten_dang_nhap = '$ten_dang_nhap' and mat_khau = '$mat_khau'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function danh_sach($vi_tri, $so_luong)
	{
		$vi_tri = intval($vi_tri);
		$so_luong = intval($so_luong);
		$sql = "select * from thanh_vien limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}
	function so_luong()
	{
		$sql = "select count(*) from thanh_vien";
		$this->setQuery($sql);
		return $this->loadResult();
	}
	function xem($ma)
	{
		$ma = intval($ma);

		$sql = "select * from thanh_vien where ma = '$ma'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xem_cmnd($cmnd)
	{
		$cmnd = addslashes($cmnd);

		$sql = "select * from thanh_vien where cmnd = '$cmnd'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xem_email($email)
	{
		$email = addslashes($email);

		$sql = "select * from thanh_vien where email = '$email'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xem_ma_khoi_tao($ma_khoi_tao)
	{
		$ma_khoi_tao = addslashes($ma_khoi_tao);

		$sql = "select * from thanh_vien where ma_khoi_tao = '$ma_khoi_tao'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function xem_ten_dn($ten_dang_nhap)
	{
		$ten_dang_nhap = addslashes($ten_dang_nhap);

		$sql = "select * from thanh_vien where ten_dang_nhap = '$ten_dang_nhap'";
		$this->setQuery($sql);
		return $this->loadRow();
	}
	function kiem_tra_kich_hoat($ten_dang_nhap,$ma_khoi_tao)
	{
		$ten_dang_nhap = addslashes($ten_dang_nhap);
		$ma_khoi_tao = addslashes($ma_khoi_tao);
		
		$sql = "select * from thanh_vien where md5(ten_dang_nhap) = '$ten_dang_nhap' and ma_khoi_tao = '$ma_khoi_tao'";
		$this->setQuery($sql);
		//echo $sql;exit;
		return $this->loadRow();
	}
	function xoa($ma)
	{
		$ma = intval($ma);

		$sql = "delete from thanh_vien where ma = '$ma'";
		$this->setQuery($sql);
		$this->query();
	}
	function them($ho_ten, $cmnd, $gioi_tinh, $ngay_sinh, $ten_dang_nhap, $mat_khau, $dia_chi, $email, $dien_thoai, $cong_ty, $ma_khoi_tao, $trang_thai)
	{
		$ho_ten = addslashes($ho_ten);
		$cmnd = intval($cmnd);
		$ten_dang_nhap = addslashes($ten_dang_nhap);
		$mat_khau = md5($mat_khau);
		$dia_chi = addslashes($dia_chi);
		$email = addslashes($email);
		$dien_thoai = addslashes($dien_thoai);
		$cong_ty = addslashes($cong_ty);
		$ma_khoi_tao = addslashes($ma_khoi_tao);
		$trang_thai = intval($trang_thai);
		$ngay_sinh=substr($ngay_sinh,6,4).'-'.substr($ngay_sinh,0,2).'-'.substr($ngay_sinh,3,2);

		$sql = "INSERT INTO thanh_vien(ho_ten,cmnd,gioi_tinh,ngay_sinh,ten_dang_nhap,mat_khau,dia_chi,email,dien_thoai,cong_ty,ma_khoi_tao,trang_thai) VALUES ('$ho_ten','$cmnd','$gioi_tinh','$ngay_sinh','$ten_dang_nhap','$mat_khau','$dia_chi','$email','$dien_thoai','$cong_ty','$ma_khoi_tao','$trang_thai')";
		$this->setQuery($sql);
		return $this->query();
	}
	function cap_nhat($ma, $ho_ten, $gioi_tinh, $ngay_sinh, $dia_chi, $email, $dien_thoai, $cong_ty)
	{
		$ma = intval($ma);
		$ho_ten = addslashes($ho_ten);
		$dia_chi = addslashes($dia_chi);
		$email = addslashes($email);
		$dien_thoai = addslashes($dien_thoai);
		$cong_ty = addslashes($cong_ty);
		$ngay_sinh=substr($ngay_sinh,6,4).'-'.substr($ngay_sinh,0,2).'-'.substr($ngay_sinh,3,2);

		$sql ="UPDATE thanh_vien SET ho_ten= '$ho_ten',gioi_tinh= '$gioi_tinh', ngay_sinh= '$ngay_sinh', dia_chi= '$dia_chi', email= '$email', dien_thoai= '$dien_thoai', cong_ty= '$cong_ty' WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function cap_nhat_tt($ma)
	{
		$ma = intval($ma);
		$sql ="UPDATE thanh_vien SET trang_thai= 1-trang_thai WHERE ma = '$ma'";
		//echo $sql;exit;
		$this->setQuery($sql);
		return $this->query();
	}		
	function cap_nhat_ma_khoi_tao($ma)
	{
		$ma = intval($ma);

		$sql = "UPDATE thanh_vien SET ma_khoi_tao='0' where ma = '$ma'";
		$this->setQuery($sql);
		return $this->loadRow();
	}	
	function cap_nhat_mat_khau($ma, $mat_khau_moi)
	{
		$ma = intval($ma);
		$mat_khau_moi = md5(addslashes($mat_khau_moi));
		$sql ="UPDATE thanh_vien SET mat_khau='$mat_khau_moi' WHERE ma = '$ma'";
		$this->setQuery($sql);
		return $this->query();
	}
	function tim_kiem ($tu_khoa)
	{
		$sql = "select * from thanh_vien where	$ho_ten like %tu_khoa% or 	$ten_dang_nhap like %tu_khoa% or 	$mat_khau like %tu_khoa% or 	$dia_chi like %tu_khoa% or 	$email like %tu_khoa% or 	$dien_thoai like %tu_khoa% or 	$cong_ty like %tu_khoa% or 	$ma_khoi_tao like %tu_khoa%"; 
		$this->setQuery($sql);
		return $this->loadAllRow();	
	}
 }
?>